<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Statistics extends CI_Controller{
    
    // Mang tham so nhan duoc o moi action
    protected $_arrParam;
    
    // Model
    protected $_Model;
    
    // pagination
    protected $_per_page = 12;
    
    public function __construct(){
        parent::__construct();
        require_once(APPPATH . 'modules/backend/autoload.php');
		$this->load->language('backend');
		$this->smarty->assign('language', $this->lang);
		$this->smarty->assign('message',$this->session->flashdata('message'));
		$this->smarty->assign('error_message',$this->session->flashdata('error_message'));
        require_once(APPPATH . 'modules/backend/denied.php');
        
        $this->_Model = $this->db_statistics_model;
        
        $this->load->library('pagination');
    }
    
    /** *********************************** Thống kê  tổng doanh thu giao dịch */
    public function totalrevenueExchange(){
        
        $jqueryui_datepicker = array(base_url() . 'jscripts/jquery-ui/jquery.ui.datepicker.js');
        $jqueryuiCore = array(
                            base_url() . 'jscripts/jquery-ui/jquery.ui.core.js',
                            base_url().'layout/template/backend/js/action.statistics.js'
        );
        
        $this->smarty->assign('jqueryuiCore', $jqueryuiCore);
        $this->smarty->assign('jqueryui_datepicker', $jqueryui_datepicker);
        
        $this->smarty->assign('title','Admin Panel - Xem doanh thu');
        $this->smarty->assign('heading','THỐNG KÊ DOANH THU GIAO DỊCH');
        
        /** Grid thống kê giao dịch */
                
        $config['base_url'] = base_url(). "admin/statistics/ajax_grid_bantien/page";
        $config['total_rows'] = $this->_Model->count_all($this->_arrParam, array('task' => 'ban-tien'))->num_rows();
        $limit = $config['per_page'] = $this->_per_page;
        $start = $this->uri->segment(5);
        $config['uri_segment'] = 5;
        $config["num_links"] = 3;
        
        $this->pagination->initialize($config);

        $pagination = $this->pagination->create_links();

        $result = $this->_Model->actionStatistics($this->_arrParam, array('task' => 'ban-tien'),$limit,$start);
        $this->smarty->assign('result', $result);
        $this->smarty->assign('pagination', $pagination);

        $this->smarty->view('backend/statistics/revenue_exchange', 'grid');
        $this->smarty->display('backend/admin');
    }
    
    public function ajax_grid_bantien(){
        if(IS_AJAX){
            $config['base_url'] = base_url(). "admin/statistics/ajax_grid_bantien/page";
            $config['total_rows'] = $this->_Model->count_all($this->_arrParam, array('task' => 'ban-tien'))->num_rows();
            $limit = $config['per_page'] = $this->_per_page;
            $start = $this->uri->segment(5);
            $config['uri_segment'] = 5;
            $config["num_links"] = 3;

            $this->pagination->initialize($config);
    
            $pagination = $this->pagination->create_links();
            $result = $this->_Model->actionStatistics($this->_arrParam, array('task' => 'ban-tien'),$limit,$start);
            
            if(isset($this->_arrParam['from']) && isset($this->_arrParam['to'])){
                $date = "từ ngày ".convertDateTime($this->_arrParam['from'])." đến ngày ".convertDateTime($this->_arrParam['to']);
            }else{$date = "";}
            
            echo '
            <div class="title-statistics">Bảng thống kê giao dịch <strong>BẮN TIỀN</strong> '.$date.'</div>
            <table class="table-grid-statistics" border="1">
                <tr>
                    <th>Ngày</th>
                    <th>Số tiền</th>
                </tr>';
                if(count($result)>0){
                    foreach ($result as $item){
                    echo '<tr>';
                        echo '<td>'.$item->date.'</td>';
                        echo '<td>'.vndformat($item->amount).' VNĐ</td>';
                    echo '</tr>';
                    }
                }else{
                    echo '<tr>';
                        echo '<td colspan="2" align="center">No data to display</td>';
                    echo '</tr>';
                }
                echo '
                <tr style="background:#F9F9F9">
                    <td colspan="2">
                        <div class="grid-column-pager">
                            <div class="current-pager">
                                <div id="loading-pager"><img src="'.base_url().'layout/template/images/fbloading.gif" />Đang tải...</div>
                                '.$pagination.'
                            </div>
                        </div>
                    </td>
                </tr>
           </table>';
        }
    }
    
    public function ajax_grid_muathe(){
        if(IS_AJAX){
            $config['base_url'] = base_url(). "admin/statistics/ajax_grid_muathe/page";
            $config['total_rows'] = $this->_Model->count_all($this->_arrParam, array('task' => 'mua-the'))->num_rows();
            $limit = $config['per_page'] = $this->_per_page;
            $start = $this->uri->segment(5);
            $config['uri_segment'] = 5;
            $config["num_links"] = 3;
            
            $this->pagination->initialize($config);
    
            $pagination = $this->pagination->create_links();
            $result = $this->_Model->actionStatistics($this->_arrParam, array('task' => 'mua-the'),$limit,$start);
            
            if(isset($this->_arrParam['from']) && isset($this->_arrParam['to'])){
                $date = "từ ngày ".convertDateTime($this->_arrParam['from'])." đến ngày ".convertDateTime($this->_arrParam['to']);
            }else{$date = "";}
            
            echo '
            <div class="title-statistics">Bảng thống kê giao dịch <strong>MUA THẺ</strong> '.$date.'</div>
            <table class="table-grid-statistics" border="1">
                <tr>
                    <th>Ngày</th>
                    <th>Số tiền</th>
                </tr>';
                if(count($result)>0){
                    foreach ($result as $item){
                    echo '<tr>';
                        echo '<td>'.$item->date.'</td>';
                        echo '<td>'.vndformat($item->amount).' VNĐ</td>';
                    echo '</tr>';
                    }
                }else{
                    echo '<tr>';
                        echo '<td colspan="2" align="center">No data to display</td>';
                    echo '</tr>';
                }
                echo '
                <tr style="background:#F9F9F9">
                    <td colspan="2">
                        <div class="grid-column-pager">
                            <div class="current-pager">
                                <div id="loading-pager"><img src="'.base_url().'layout/template/images/fbloading.gif" />Đang tải...</div>
                                '.$pagination.'
                            </div>
                        </div>
                    </td>
                </tr>
           </table>';
        }
    }
    
    public function ajax_grid_thanhtoan(){
        if(IS_AJAX){
            $config['base_url'] = base_url(). "admin/statistics/ajax_grid_thanhtoan/page";
            $config['total_rows'] = $this->_Model->count_all($this->_arrParam, array('task' => 'thanh-toan'))->num_rows();
            $limit = $config['per_page'] = $this->_per_page;
            $start = $this->uri->segment(5);
            $config['uri_segment'] = 5;
            $config["num_links"] = 3;
            
            $this->pagination->initialize($config);
    
            $pagination = $this->pagination->create_links();
            $result = $this->_Model->actionStatistics($this->_arrParam, array('task' => 'thanh-toan'),$limit,$start);
            
            if(isset($this->_arrParam['from']) && isset($this->_arrParam['to'])){
                $date = "từ ngày ".convertDateTime($this->_arrParam['from'])." đến ngày ".convertDateTime($this->_arrParam['to']);
            }else{$date = "";}
            
            echo '
            <div class="title-statistics">Bảng thống kê giao dịch <strong>THANH TOÁN</strong> '.$date.'</div>
            <table class="table-grid-statistics" border="1">
                <tr>
                    <th>Ngày</th>
                    <th>Số tiền</th>
                </tr>';
                if(count($result)>0){
                    foreach ($result as $item){
                    echo '<tr>';
                        echo '<td>'.$item->date.'</td>';
                        echo '<td>'.vndformat($item->amount).' VNĐ</td>';
                    echo '</tr>';
                    }
                }else{
                    echo '<tr>';
                        echo '<td colspan="2" align="center">No data to display</td>';
                    echo '</tr>';
                }
                echo '
                <tr style="background:#F9F9F9">
                    <td colspan="2">
                        <div class="grid-column-pager">
                            <div class="current-pager">
                                <div id="loading-pager"><img src="'.base_url().'layout/template/images/fbloading.gif" />Đang tải...</div>
                                '.$pagination.'
                            </div>
                        </div>
                    </td>
                </tr>
           </table>';
        }
    }
    
    /** Biểu đồ thống kê giao dịch */
    public function Chartfirst(){
        $result = $this->_Model->count_all($this->_arrParam, array('task' => 'ban-tien'))->result();
        if(count($result) > 0){
            $data[0] = array('day','Bắn tiền');
            foreach($result as $row){
                $data[] = array(convertDateTime($row->date),(int)$row->amount);
            }
            echo json_encode($data);
        }
    }
    
    public function jsonChartBantien(){       
        if(IS_AJAX){
            $result = $this->_Model->count_all($this->_arrParam, array('task' => 'ban-tien'))->result();
            if(count($result) > 0){
                $data[0] = array('day','Bắn tiền');
                foreach($result as $row){
                    $data[] = array(convertDateTime($row->date),(int)$row->amount);
                }
                echo json_encode($data);
            }
            
            $user = $this->login_manager->get_user();
            $aParamAdminLog=array(
                            'username'   => $user[0]->name,
                            'customerID' => $user[0]->customerID,
                            'email'      => $user[0]->email,
                            'lastLogin'  => date("Y-m-d H:i:s"),
                            'logAction'  => "Thống kê giao dịch : Bắn tiền",
                            'ip'         => $_SERVER['REMOTE_ADDR'],
                            'uID'        => $user[0]->uID
                            );
            $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
        }
    }
    
    public function jsonChartMuathe(){
        if(IS_AJAX){
            $result = $this->_Model->count_all($this->_arrParam, array('task' => 'mua-the'))->result();
            if(count($result) > 0){
                $data[0] = array('day','Mua thẻ');
                foreach($result as $row){
                    $data[] = array(convertDateTime($row->date),(int)$row->amount);
                }
                echo json_encode($data);
            }
            
            $user = $this->login_manager->get_user();
            $aParamAdminLog=array(
                            'username'   => $user[0]->name,
                            'customerID' => $user[0]->customerID,
                            'email'      => $user[0]->email,
                            'lastLogin'  => date("Y-m-d H:i:s"),
                            'logAction'  => "Thống kê giao dịch : Mua thẻ",
                            'ip'         => $_SERVER['REMOTE_ADDR'],
                            'uID'        => $user[0]->uID
                            );
            $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
        }
    }
    
    public function jsonChartThanhtoan(){
        if(IS_AJAX){
            $result = $this->_Model->count_all($this->_arrParam, array('task' => 'thanh-toan'))->result();
            if(count($result) > 0){
                $data[0] = array('day','Thanh toán');
                foreach($result as $row){
                    $data[] = array(convertDateTime($row->date),(int)$row->amount);
                }
                echo json_encode($data);
            }
            
            $user = $this->login_manager->get_user();
            $aParamAdminLog=array(
                            'username'   => $user[0]->name,
                            'customerID' => $user[0]->customerID,
                            'email'      => $user[0]->email,
                            'lastLogin'  => date("Y-m-d H:i:s"),
                            'logAction'  => "Thống kê giao dịch : Thanh toán",
                            'ip'         => $_SERVER['REMOTE_ADDR'],
                            'uID'        => $user[0]->uID
                            );
            $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);
        }
    }
    
    /** Export file Excel */
    function cellColor($cells,$color){/** Color */
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()
        ->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array('rgb' => $color)
        ));
    }
    
    /** ===== Export file Excel theo tổng ===== */
    public function ExporttoExcel(){
        
        $this->load->library('Classes/PHPExcel');
        
        if(isset($this->_arrParam['from'])){
            $from = convertDateTime($this->_arrParam['from']);
        }
        if(isset($this->_arrParam['to'])){
            $to = convertDateTime($this->_arrParam['to']);
        }
        
        if(isset($this->_arrParam['check'])){
            $check = $this->_arrParam['check'];
        }
        
        $options = $this->_arrParam['options'];
        $data = null;
        $name = null;
        $header = null;
        
        /** Export file Excel trong ngày  */
        if($options == "mt_trongngay"){
            $data = $this->_Model->ExportfileSumDate(array('task'=>'mua-the'));
            $header = "MUA THẺ TRONG NGÀY ".gmdate("d-m-Y",time()+7*3600);
            $name = "gdmt_trongngay_";
            
        }elseif($options == "bt_trongngay"){
            $data = $this->_Model->ExportfileSumDate(array('task'=>'ban-tien'));
            $header = "BẮN TIỀN TRONG NGÀY ".gmdate("d-m-Y",time()+7*3600);
            $name = "gdbt_trongngay_";
            
        }elseif($options == "tt_trongngay"){
            $data = $this->_Model->ExportfileSumDate(array('task'=>'thanh-toan'));
            $header = "THANH TOÁN TRONG NGÀY ".gmdate("d-m-Y",time()+7*3600);
            $name = "gdtt_trongngay_";
        
        /** Export file Excel trong tuần  */   
        }elseif($options == "mt_trongtuan"){
            $data = $this->_Model->ExportfileSumWeek(array('task'=>'mua-the'));
            $header = "MUA THẺ TRONG TUẦN";
            $name = "gdmt_trongtuan_";
            
        }elseif($options == "bt_trongtuan"){
            $data = $this->_Model->ExportfileSumWeek(array('task'=>'ban-tien'));
            $header = "BẮN TIỀN TRONG TUẦN";
            $name = "gdbt_trongtuan_";
            
        }elseif($options == "tt_trongtuan"){
            $data = $this->_Model->ExportfileSumWeek(array('task'=>'thanh-toan'));
            $header = "THANH TOÁN TRONG TUẦN";
            $name = "gdtt_trongtuan_";
        
        /** Export file Excel trong tháng  */    
        }elseif($options == "mt_trongthang"){
            $data = $this->_Model->ExportfileSumMonth(array('task'=>'mua-the'));
            $header = "MUA THẺ TRONG THÁNG ".gmdate("m-Y",time()+7*3600);
            $name = "gdmt_trongthang_";
            
        }elseif($options == "bt_trongthang"){
            $data = $this->_Model->ExportfileSumMonth(array('task'=>'ban-tien'));
            $header = "BẮN TIỀN TRONG THÁNG ".gmdate("m-Y",time()+7*3600);
            $name = "gdbt_trongthang_";
            
        }elseif($options == "tt_trongthang"){
            $data = $this->_Model->ExportfileSumMonth(array('task'=>'thanh-toan'));
            $header = "THANH TOÁN TRONG THÁNG ".gmdate("m-Y",time()+7*3600);
            $name = "gdtt_trongthang_";
        
        /** Export file Excel trong năm  */    
        }elseif($options == "mt_trongnam"){
            $data = $this->_Model->ExportfileSumYear(array('task'=>'mua-the'));
            $header = "MUA THẺ TRONG NĂM ".gmdate("Y",time()+7*3600);
            $name = "gdmt_trongnam_";
            
        }elseif($options == "bt_trongnam"){
            $data = $this->_Model->ExportfileSumYear(array('task'=>'ban-tien'));
            $header = "BẮN TIỀN TRONG NĂM ".gmdate("Y",time()+7*3600);
            $name = "gdbt_trongnam_";
            
        }elseif($options == "tt_trongnam"){
            $data = $this->_Model->ExportfileSumYear(array('task'=>'thanh-toan'));
            $header = "THANH TOÁN TRONG NĂM ".gmdate("Y",time()+7*3600);
            $name = "gdtt_trongnăm_";
        
        /** Export file Excel từ ngày ... đến ngày ...  */    
        }elseif($options == "mt_from_to"){
            $data = $this->_Model->count_all($this->_arrParam,array('task'=>'mua-the'))->result();
            $header = "MUA THẺ TỪ NGÀY ".$from." ĐẾN ".$to;
            $name = "gdmt_muathe_".$from."-".$to."_";
            
        }elseif($options == "bt_from_to"){
            $data = $this->_Model->count_all($this->_arrParam,array('task'=>'ban-tien'))->result();
            $header = "BẮN TIỀN TỪ NGÀY ".$from." ĐẾN ".$to;
            $name = "gdbt_bantien_".$from."-".$to."_";
            
        }elseif($options =="tt_from_to"){
            $data = $this->_Model->count_all($this->_arrParam,array('task'=>'thanh-toan'))->result();
            $header = "THANH TOÁN TỪ NGÀY ".$from." ĐẾN ".$to;
            $name = "gdtt_thanhtoan_".$from."-".$to."_";
            
        }elseif($data == ""){
            return false;
        }
        
        /** ================================= Export theo list =================================*/
        if(isset($check)){
            /** Export file Excel trong ngày  */
            if($options == "mt_trongngay"){
                $data = $this->_Model->ExportfileListDate(array('task'=>'mua-the'));
                
            }elseif($options == "bt_trongngay"){
                $data = $this->_Model->ExportfileListDate(array('task'=>'ban-tien'));
                
            }elseif($options == "tt_trongngay"){
                $data = $this->_Model->ExportfileListDate(array('task'=>'thanh-toan'));
            
            /** Export file Excel trong tuần  */   
            }elseif($options == "mt_trongtuan"){
                $data = $this->_Model->ExportfileListWeek(array('task'=>'mua-the'));
                
            }elseif($options == "bt_trongtuan"){
                $data = $this->_Model->ExportfileListWeek(array('task'=>'ban-tien'));
                
            }elseif($options == "tt_trongtuan"){
                $data = $this->_Model->ExportfileListWeek(array('task'=>'thanh-toan'));
            
            /** Export file Excel trong tháng  */    
            }elseif($options == "mt_trongthang"){
                $data = $this->_Model->ExportfileListMonth(array('task'=>'mua-the'));
                
            }elseif($options == "bt_trongthang"){
                $data = $this->_Model->ExportfileListMonth(array('task'=>'ban-tien'));
                
            }elseif($options == "tt_trongthang"){
                $data = $this->_Model->ExportfileListMonth(array('task'=>'thanh-toan'));
            
            /** Export file Excel trong năm  */    
            }elseif($options == "mt_trongnam"){
                $data = $this->_Model->ExportfileListYear(array('task'=>'mua-the'));
                
            }elseif($options == "bt_trongnam"){
                $data = $this->_Model->ExportfileListYear(array('task'=>'ban-tien'));
                
            }elseif($options == "tt_trongnam"){
                $data = $this->_Model->ExportfileListYear(array('task'=>'thanh-toan'));
            
            /** Export file Excel từ ngày ... đến ngày ...  */    
            }elseif($options == "mt_from_to"){
                $data = $this->_Model->ExportfileByDate($this->_arrParam,array('task'=>'mua-the'));
                
            }elseif($options == "bt_from_to"){
                $data = $this->_Model->ExportfileByDate($this->_arrParam,array('task'=>'ban-tien'));
                
            }elseif($options =="tt_from_to"){
                $data = $this->_Model->ExportfileByDate($this->_arrParam,array('task'=>'thanh-toan'));
                
            }
        }
  
        $objPHPExcel = new PHPExcel();
        
        /** Merge Cell */
        $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:C2');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', 'BẢNG THỐNG KÊ GIAO DỊCH '.$header);
        /** Alignment */
        $objPHPExcel->getActiveSheet()->getStyle('A1')
                                    ->getAlignment()
                                    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')
                                    ->getAlignment()
                                    ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        /** Create border title*/
        $styleArray = array(
    	  'borders' => array(
    		'allborders' => array(
    		  'style' => PHPExcel_Style_Border::BORDER_THIN,
              'color' => array('rgb' => '60AAD2'),
    		)
    	  )
    	);
    	$objPHPExcel->getActiveSheet()->getStyle('A1:C3')->applyFromArray($styleArray);
        unset($styleArray);
        
        /** Set font & font-size*/
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('Times New Roman');
       	$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(13);
        
        if(!empty($data)){
            $i=4;
            foreach($data as $value){
    
                /** Set font & font-size*/
                $objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getFont()->setName('Times New Roman');
            	$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getFont()->setSize(12);
            	$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getFont()->setName('Times New Roman');
            	$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getFont()->setSize(12);
            	$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setName('Times New Roman');
            	$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getFont()->setSize(12);
                
                /** Set width column */
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(35);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
                /** Create header */
                for($j=3;$j<=3;$j++){
                    $objPHPExcel->getActiveSheet()->getStyle('A'.$j)->getFont()->setSize(10);
                    $objPHPExcel->getActiveSheet()->getStyle('B'.$j)->getFont()->setSize(10);
                    $objPHPExcel->getActiveSheet()->getStyle('C'.$j)->getFont()->setSize(10);
                    $objPHPExcel->getActiveSheet()->getStyle('A'.$j)->getFont()->setBold(true);
                    $objPHPExcel->getActiveSheet()->getStyle('B'.$j)->getFont()->setBold(true);
                    $objPHPExcel->getActiveSheet()->getStyle('C'.$j)->getFont()->setBold(true);
                    $objPHPExcel->getActiveSheet()->getStyle('A'.$j)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                    $objPHPExcel->getActiveSheet()->getStyle('A'.$j)->getFill()->getStartColor()->setRGB('E6FEF8');
                    $objPHPExcel->getActiveSheet()->getStyle('B'.$j)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                    $objPHPExcel->getActiveSheet()->getStyle('B'.$j)->getFill()->getStartColor()->setRGB('E6FEF8');
                    $objPHPExcel->getActiveSheet()->getStyle('C'.$j)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                    $objPHPExcel->getActiveSheet()->getStyle('C'.$j)->getFill()->getStartColor()->setRGB('E6FEF8');
                    $objPHPExcel->getActiveSheet()->setCellValue('A'.$j, 'NGÀY')
                                        		->setCellValue('B'.$j, 'SỐ TIỀN')
                                        		->setCellValue('C'.$j, 'KIỂU GIAO DỊCH');
                }
                
                /** Data */
                $kieugd = null;
                if($value->typeUse == "muathe"){
                    $kieugd = "Mua thẻ";
                }elseif($value->typeUse == "bantien"){
                    $kieugd = "Bắn tiền";
                }if($value->typeUse == "thanhtoan"){
                    $kieugd = "Thanh toán";
                }
                $objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->getActiveSheet()
                                            ->getRowDimension($i)
                                            ->setRowHeight(20);
                $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $value->date)
                                        	->setCellValue('B'.$i, $value->amount)
                                        	->setCellValue('C'.$i, $kieugd);
                
                /** Create border */
                $styleArray = array(
            	  'borders' => array(
            		'allborders' => array(
            		  'style' => PHPExcel_Style_Border::BORDER_THIN,
                      'color' => array('rgb' => '60AAD2'),
            		)
            	  )
            	);
            	$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->applyFromArray($styleArray);
            	$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->applyFromArray($styleArray);
            	$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->applyFromArray($styleArray);
                unset($styleArray);
                $i++;
            }
        }else{
            $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(90);
            $objPHPExcel->getActiveSheet()->setCellValue('A3', "No data to display");
        }
        
        $fileName = $name.gmdate("dmY_His", time() + 7*3600).".xls";
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$fileName.'"');
        header('Cache-Control: max-age=0');
        
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        //print_r($this->_arrParam);
        
        $user = $this->login_manager->get_user();
        $aParamAdminLog=array(
                        'username'   => $user[0]->name,
                        'customerID' => $user[0]->customerID,
                        'email'      => $user[0]->email,
                        'lastLogin'  => date("Y-m-d H:i:s"),
                        'logAction'  => "Thống kê giao dịch : ".$kieugd." : Export File ".$fileName,
                        'ip'         => $_SERVER['REMOTE_ADDR'],
                        'uID'        => $user[0]->uID
                        );
        $log = $this->db_user_model->insertUserAdminLog($aParamAdminLog);

    }
 
}

/* End of file statistics.php */
/* Location: ./system/application/controllers/backend/statistics.php */